<?
if (!defined ('L_MOD')) {die("<h3>You can`t access this file...</h3>");}

include("./php/spaw2/spaw.inc.php");

$old_val = array('\"', 'http://');
$new_val   = array('"', '');
$trans = get_html_translation_table(HTML_ENTITIES);
$trans = array_flip($trans);
$error=0; $error_key=0; $err_cat=0; $err_url=0; $err_name=0; $err_short=0; $err_double=0;
$err_message_beg='<center><b><font color="#FF0000">'._ERROR_.'</font>&nbsp;';
$err_message_end='</b></center>';

if ($_SESSION['auth']!=1)
	$AuthMes='<font size="-3" color="#FF0000">'._WCCATWARNING_.':</font> <font size="-3">'._WCCATAUTHMES_.'</font><br><br>';
else
	$AuthMes='';
	
if (isset($cid) && is_numeric($cid) && $cid!='' && $cid>0)
{
	$cat_id=$cid;
	$refreshadd='fla=category&cid='.$cid.'';
}else
{
	$refreshadd='fla=index';
	$cat_id='';
}

if ($wccat_allowadd==0)
{
	print '<br><br><center><b><font color="#FF0000">'._ERROR_.'</font> '._ERRORADDALLOW_.'</b></center>';
	echo "<meta http-equiv='refresh' content='2; url=/index.php?nma=".$nma."&".$refreshadd."'>";
}else
{
	if(!empty($_POST))
	{
		$check_url=$new_url;
		$check_url=str_replace($old_val, $new_val, $check_url);
		$check_url='http://'.$check_url;
		$parsed = parse_url($check_url);
		$check_url=$parsed['host'];
		$check_url=str_replace($old_val, $new_val, $check_url);
		
		$check_rurl=$new_rurl;
		$check_rurl=str_replace($old_val, $new_val, $check_rurl);
		
		$check_name=$new_name;

		$check_short = strtr($new_short, $trans);
		$check_short = str_replace($old_val, $new_val, $check_short);
		
		$check_full = strtr($new_full, $trans);
		$check_full = str_replace($old_val, $new_val, $check_full);
		
		if($check_full==_FULLWARNING_)
			$check_full="";
		
		if(!empty($_SESSION['registr_key']) && $_SESSION['registr_key']==$_POST['vote_key'])
		{
			$_SESSION['registr_key']=null;
			$error_key=0;
		}else
			$error_key=1;
		
		if (!empty($cid) && $cid>0)
			$err_cat=0;
		else
			$err_cat=1;

		if (!empty($check_url) && $check_url!='')
			$err_url=0;
		else
			$err_url=1;
		
		if (!empty($check_name) && $check_name!='')
			$err_name=0;
		else
			$err_name=1;
		
		if (empty($check_short) || $check_short=='<br />')
			$err_short=1;
		else
			$err_short=0;
		print "<br>";
		if ($error_key!=0)
			print $err_message_beg._ERRKEYMES_.$err_message_end;
		if ($err_cat!=0)
			print $err_message_beg._ERRCATMES_.$err_message_end;
		if ($err_url!=0)
			print $err_message_beg._ERRURLMES_.$err_message_end;
		if ($err_name!=0)
			print $err_message_beg._ERRNAMEMES_.$err_message_end;
		if ($err_short!=0)
			print $err_message_beg._ERRSHORTMES_.$err_message_end;
		
		$error=$error_key+$err_cat+$err_url+$err_name+$err_short;
		if ($error==0)
		{
			$check_url=strtolower($check_url);
			$wwwurl=substr($check_url, 0, 4);
			if ($wwwurl=="www.")
			{
				$nQ=substr($check_url, 4);
				$Q_add="OR mod_url='".$nQ."'";
			}else
				$Q_add="OR mod_url='www.".$check_url."'";
			$check_double=mysql_num_rows(mysql_query("SELECT mod_id FROM ${wcpref}".$nma." WHERE mod_url='".$check_url."' ".$Q_add.""));
		}
		if ($check_double!=0)
			$err_double=1;
		else
			$err_double=0;
		
		if ($err_double!=0)
			print $err_message_beg._ERRDOUBLEMES_.$err_message_end;
			
		$error=$error_key+$err_cat+$err_url+$err_name+$err_short+$err_double;
	}
	
	if($error>0 || empty($_POST))
	{
		if (empty($_POST))
		{
			$check_short='';
			$check_url='';
			$check_rurl='';
			$check_name='';
		}
	
		if (empty($_POST) || $check_full=="")
			$check_full=_FULLWARNING_;
		
		$spaw1 = new SpawEditor('new_short');

		$spaw1->hideModeStrip();
		$spaw1->addToolbars("edit", "format", "my_insert", "table", "font", "style", "tools");
		
		$spaw1->addPage(new SpawEditorPage("new_short",(_TYPESHORT_."*"),$check_short)); // left-to-right direction is assumed by default
		$spaw1->addPage(new SpawEditorPage("new_full",_TYPEFULL_,$check_full));
		
		$query_cat=mysql_query("SELECT * FROM ${wcpref}".$nma."_category ORDER BY mod_category ASC");

		$quik_select='<select name="cid">';
			while($result_cat=mysql_fetch_array($query_cat))
			{
				extract($result_cat);
				if ($mod_id==$cid)
				{
					$mod_18_new=$mod_18;
					$name_category=$mod_category;
					$select='selected';
				}else
					$select='';
				if($wccat_allow18plus==0 && $mod_18==1)
				{
				}else
					$quik_select.='	<option value="'.$mod_id.'" '.$select.'>'.$mod_category;
			}
			$quik_select.='</select>';
		if(!isset($name_category))
			$name_category='';
		print '<h2>'._ADDSITEOTHERTITLE_.': <a href="?nma='.$nma.'&fla=category&cid='.$cid.'">'.$name_category.'</a></h2><br>';

		print $AuthMes;
		print '<form name="form" method="post" action="'.$portal_subdir.'/index.php?nma='.$nma.'&fla='.$fla.'">
			<table align="center" cellpadding="1" cellspacing="1" border="0" width="98%">
				<tr>
					<td colspan="2" align="center"><font color="#0000FF">'._STARMESSAGE_.'</font></td>
				</tr>
				<tr class="odd">
					<td align="left" colspan="2">'.view_vote_keys().'</td>
				</tr>
				<tr class="even">
					<td align="left" width="20%">'._SELECTCAT_.'*&nbsp;&nbsp;</td>
					<td align="left">'.$quik_select.'</td>
				</tr>
				<tr class="odd">
					<td align="left">'._TYPEURL_.'*&nbsp;&nbsp;</td>
					<td align="left"><input type="text" name="new_url" size="33" value="'.$check_url.'">&nbsp;<font size="-3" color="#0000FF">('._WITHOUTHTTP_.')</font></td>
				</tr>
				<tr class="even">
					<td align="left">'._TYPERURL_.'&nbsp;&nbsp;</td>
					<td align="left">
						<input type="text" name="new_rurl" size="33" value="'.$check_rurl.'">&nbsp;<font size="-3" color="#0000FF">('._WITHOUTHTTP_.')</font> <a href="#" onclick="choose(\'revercecode\')">'._SHOWREVERCE_.'</a>
						<div id="revercecode" style="display:none;"><textarea rows="3" readonly>'.$wccat_revercecode.'</textarea></div>
					</td>
				</tr>
				<tr class="odd">
					<td align="left">'._TYPENAME_.'*&nbsp;&nbsp;</td>
					<td align="left">
						<input type="text" name="new_name" value="'.$check_name.'" size="33" id="tname" onkeyup="Last50(this, 30, \'sname\', \'tname\');">&nbsp;<font size="-3" color="#0000FF">('._NAMETIPS_.')</font>
						<font color="#808080"><div id="sname">30</div></font>
					</td>
				</tr>
				<tr class="even">
					<td align="center" colspan="2">
						'._TYPEDESCRIPT_.'
					</td>
				</tr>
				<tr>
					<td align="center" colspan="2">';
						$spaw1->show();
					print'</td>
				</tr>
				<tr>
					<td align="center" colspan="2">
						<input type="submit" value="'._ADD_.'">
					</td>
				</tr>
			</table>
		</form>';
	}else
	{
		if ($_SESSION['user_id']=='')
			$userId=-1;
		else
			$userId=$_SESSION['user_id'];
		$query="INSERT INTO ${wcpref}".$nma." VALUES (
			NULL,
			'".$cid."', 
			'".$check_url."',
			'".$check_name."',
			'".$check_short."',
			'".$check_full."',
			'".$wccat_block."',
			'0',
			'0',
			'0',
			'".$userId."',
			'".$check_rurl."',
			'',
			'',
			'',
			'',
			'0',
			now()
		)";
		if (mysql_query($query))
		{
			print "<center><br><br>"._WAITMESSAGE_."</center>";
			echo "<meta http-equiv='refresh' content='0; url=/index.php?nma=".$nma."&".$refreshadd."'>";
		}else
		{
			print '<br><br><center><b><font color="#FF0000">'._ERROR_.'</font> '._ERRORADDQUERY_.'</b></center>';
			echo "<meta http-equiv='refresh' content='2; url=/index.php?nma=".$nma."&".$refreshadd."'>";
		}
	}
}
?>

<script language="JavaScript">
	function Last50(m, limit, idsname, idtname)
	{
		var NameSize=m.value.length;
		var LastSize=limit-NameSize;
		var NameValue="";
		document.getElementById(idsname).innerHTML=LastSize;
					
		if (NameSize>limit)
		{
			document.getElementById(idtname).value=document.getElementById(idtname).value.substr(0,limit);
			document.getElementById(idsname).innerHTML="0";
			return;
		}
	}
	
	function choose(id)
    {
		if (document.getElementById(id).style.display == 'none')
			show(id);
		else if(document.getElementById(id).style.display == 'block')
			hide(id);
	}

	function hide(id)
	{
		document.getElementById(id).style.display = 'none';
	}

	function show(id)
	{
		document.getElementById(id).style.display = 'block';
	}

</script>



